<?php
class Orders extends MX_Controller
{
    function __construct()
    {
        parent::__construct();        
        $this->load->library('pagination');        
    }
    
    function index($res = '~', $keyword='~',$row=0)
    {   

        $order = new Order();
        $data['restaurent'] = $res;
        if($this->input->post('restaurent_option'))
        {
            $data['restaurent'] = $this->input->post('restaurent_option');
        }
        
        $data['key_word'] = urldecode($keyword);
        if($this->input->post('txtKeyWord'))
        {
            $data['key_word'] = $this->input->post('txtKeyWord');
        }
        
        //paging
        include('paging.php');        		                                         
        // compute total record     
        if($data['key_word'] != '~')
        {
            $order->like('name', $data['key_word']);
        }
        if($data['restaurent'] != '~')
        {
            $order->where('restaurent_id', $data['restaurent']);
        }        
        $config['base_url']= base_url()."/administrator/orders/index/".$data['restaurent'].'/'.$data['key_word'].'/';
        $config['total_rows'] = $order->count();        
        $config['cur_page'] = $row;		
        $this->pagination->initialize($config);
        $data['list_link'] = $this->pagination->create_links();	
        
        $order = new Order();
        $order->limit($config['per_page'], $row)
            ->order_by('id','DESC');                        
        if($data['key_word'] != '~')
        {
            $order->like('name', $data['key_word']);
        }
        if($data['restaurent'] != '~')
        {
            $order->where('restaurent_id', $data['restaurent']);
        }                
        $data['list_order'] = $order->get();
        $res = new Restaurent();
        $data['list_restaurent'] = $res->get();                                
        $data['view'] = 'orders_index';
        $this->load->view('back_end/template_noright',$data);

    }
    
    function add()
    {
        if( $this->input->post('name'))
        {
            $name = $this->input->post('name');
            $restaurent_option = $this->input->post('restaurent_option');
            $customer_option = $this->input->post('customer_option');
            $status = $this->input->post('status');
            $order_shipping = $this->input->post('order_shipping');                      
                
            $order = new Order();
            $order->name = $name;
            $order->restaurent_id =$restaurent_option;            
            $order->customer_id = $customer_option;
            $order->status = $status;
            $order->create_date = date('Y-m-d H:i:s');            
            $order->order_shipping = $order_shipping;
            
            $order->save(); 
            redirect('administrator/orders');        
        } else
        {
            $res = new Restaurent();
            $data['list_restaurent'] = $res->get();
            $customer = new Customer();
            $data['list_customer'] = $customer->get();
            $data['view'] = 'orders_add';
            $this->load->view('back_end/template_noright',$data);
        }
        
    }
    function delete()
    {    
        $id = $this->input->post('param');   
        echo $id;     
        $order = new Order();
        $order->get_by_id($id);                       
        $order->delete();
            
    }		     
    function edit($id=0,$res = '~',$keyword='~',$row=0)
    {
        if( $this->input->post('name'))
        {
            $name = $this->input->post('name');
            $restaurent_option = $this->input->post('restaurent_option');
            $customer_option = $this->input->post('customer_option');
            $status = $this->input->post('status');
            $order_shipping = $this->input->post('order_shipping');                      
                
            $order = new Order();
            $order->get_by_id($id);
            $order->name = $name;
            $order->restaurent_id =$restaurent_option;            
            $order->customer_id = $customer_option;
            $order->status = $status;
            $order->modified_date = date('Y-m-d H:i:s');            
            $order->order_shipping = $order_shipping;
            
            $order->save(); 
            redirect('administrator/orders');        
        } else
        {
            $order = new Order();
            $data['restaurent'] = $res;
            if($this->input->post('restaurent_option'))
            {
                $data['restaurent'] = $this->input->post('restaurent_option');
            }
            
            $data['key_word'] = urldecode($keyword);
            if($this->input->post('txtKeyWord'))
            {
                $data['key_word'] = $this->input->post('txtKeyWord');
            }
            
            //paging
            include('paging.php');        		                                         
            // compute total record     
            if($data['key_word'] != '~')
            {
                $order->like('name', $data['key_word']);
            }
            if($data['restaurent'] != '~')
            {
                $order->where('restaurent_id', $data['restaurent']);
            }        
            $config['base_url']= base_url()."/administrator/orders/index/".$data['restaurent'].'/'.$data['key_word'].'/';
            $config['total_rows'] = $order->count();        
            $config['cur_page'] = $row;		
            $this->pagination->initialize($config);
            $data['list_link'] = $this->pagination->create_links();	
            
            $order = new Order();
            $order->limit($config['per_page'], $row)
                ->order_by('id','DESC');                        
            if($data['key_word'] != '~')
            {
                $order->like('name', $data['key_word']);
            }
            if($data['restaurent'] != '~')
            {
                $order->where('restaurent_id', $data['restaurent']);
            }                
            $data['list_order'] = $order->get();
            $res = new Restaurent();
            $data['list_restaurent'] = $res->get();
            $customer = new Customer();
            $data['list_customer'] = $customer->get();
            $order = new Order();
            $data['order'] = $order->get_by_id($id);
                                     
            $data['view'] = 'orders_edit';
            $this->load->view('back_end/template_noright',$data);
        }
    }
    
}
?>